public class BinaryTree {

    public int nodeSize = 0;
    public int leafNodeSize = 0;
    TreeNode root = this.createTree();

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.leftChildNode = B;
        A.rightChildNode = C;
        B.leftChildNode = D;
        B.rightChildNode = E;
        E.rightChildNode = H;
        C.leftChildNode = F;
        C.rightChildNode = G;

        return A;
    }

    //前序遍历：根左右
    public void preOrder(TreeNode root) {
        if (root == null)
            return;
        System.out.print(root.val); //根节点

        //一直打印，直到穷尽左节点
        preOrder(root.leftChildNode);

        preOrder(root.rightChildNode);
    }

    //中序遍历：左根右
    public void inOrder(TreeNode root) {
        if (root == null)
            return;

        inOrder(root.leftChildNode);
        System.out.print(root.val);
        inOrder(root.rightChildNode);
    }

    public void postOrder(TreeNode root) {
        if (root == null)
            return;

        postOrder(root.leftChildNode);
        postOrder(root.rightChildNode);
        System.out.print(root.val);
    }

    public int size1(TreeNode root) {
        if (root == null) {
            return 0;
        }
        nodeSize++;
        size1(root.leftChildNode);
        size1(root.rightChildNode);
        return nodeSize;
    }

    public int size2(TreeNode root) {
        if (root == null)
            return 0;

        return size2(root.leftChildNode) + size2(root.rightChildNode) + 1;
    }

    //遍历获取叶子节点的个数
    public int getLeafNodeCount1(TreeNode root) {
        if (root == null)
            return 0;

        getLeafNodeCount1(root.leftChildNode);
        getLeafNodeCount1(root.rightChildNode);

        if (root.leftChildNode == null && root.rightChildNode == null) {   //左孩子节点和右孩子节点都为空
            leafNodeSize++;
            return 0;
        }
        return leafNodeSize;
    }

    //递归获取叶子节点的个数
    public int getLeafNodeCount2(TreeNode root) {
        if (root == null) {
            return 0;
        }

        if (root.leftChildNode == null && root.rightChildNode == null) {   //左孩子节点和右孩子节点都为空
            return 1;
        }

        return getLeafNodeCount2(root.leftChildNode) + getLeafNodeCount2(root.rightChildNode);
    }

    public int getKLevelNodeCount(TreeNode root, int k) {
        if (root == null) {
            return 0;
        }

        if (k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.leftChildNode, k - 1) + getKLevelNodeCount(root.rightChildNode, k - 1);
    }

    public static class TreeNode {
        public char val;
        public TreeNode leftChildNode = null; // 左孩子节点
        public TreeNode rightChildNode = null;  // 右孩子节点

        public TreeNode(char val) {
            this.val = val;
        }
    }
}
